package cn.butcher21.view;

import cn.butcher21.dao.UserDao;
import cn.butcher21.pojo.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class UserController {

    @FXML
    private TableView<User> userTable;

    @FXML
    private TableColumn<User, String> t_user_name;

    @FXML
    private TableColumn<User, String> t_user_password;

    @FXML
    private Button addBtn;

    @FXML
    private TextField username;

    @FXML
    private TextField password;

    @FXML
    private Button delBtn;

    @FXML
    private Button clearBtn;

    @FXML
    private Label userTips;

    @FXML
    private void initialize() {
        loadUserTableData();
    }

    public void loadUserTableData() {
        //显示用户数据
        UserDao userDao = new UserDao();
        try {
            List<User> list = userDao.selectUserS(); // 查出所有用户
            ObservableList<User> users = FXCollections.observableArrayList();
            for (User user : list) {
                users.add(user);
            }
            t_user_name.setCellValueFactory(new PropertyValueFactory<User, String>("name"));
            t_user_password.setCellValueFactory(new PropertyValueFactory<User, String>("password"));
            userTable.setItems(users);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    @FXML
    void add(ActionEvent event) {
        if (username.getText().equals("") || password.getText().equals("")) {
            userTips.setText("用户名/密码呢?﹏?");
        } else {

            UserDao userDao = new UserDao();

            try {
                if (userDao.checkUserExist(username.getText())) {
                    String password = DigestUtils.md5Hex(this.password.getText());
                    userDao.update(username.getText(), password);
                    userTips.setText(username.getText() + "更新成功≧０≦");
                } else {
                    String password = DigestUtils.md5Hex(this.password.getText());
                    boolean add = userDao.add(username.getText(), password);
                    userTips.setText(username.getText() + "添加成功≧０≦");
                }
                loadUserTableData();
            } catch (SQLException e) {
                e.printStackTrace();
                userTips.setText("糟了，失败了≧︿≦");
            } catch (IOException e) {
                e.printStackTrace();
                userTips.setText("糟了，失败了≧︿≦");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                userTips.setText("糟了，失败了≧︿≦");
            }
        }
    }

    /**
     * 清空用户添加输入框
     *
     * @param event 事件
     */
    @FXML
    void clear(ActionEvent event) {
        userTips.setText("");
        username.setText("");
        password.setText("");
    }


    /**
     * 删除一个用户
     *
     * @param event 事件
     */
    @FXML
    void del(ActionEvent event) {
        UserDao userDao = new UserDao();
        try {
            userDao.delete(username.getText());
            userTips.setText(username.getText() + "删除成功≧０≦");

            loadUserTableData();
        } catch (SQLException e) {
            e.printStackTrace();
            userTips.setText("糟了，失败了≧︿≦");
        } catch (IOException e) {
            e.printStackTrace();
            userTips.setText("糟了，失败了≧︿≦");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            userTips.setText("糟了，失败了≧︿≦");
        }
    }

    @FXML
    void getUserSelect(MouseEvent event) {
        User user = userTable.getSelectionModel().getSelectedItem();
        username.setText(user.getName());
        password.setText(user.getPassword());
    }

}
